package com.guo.dms.common.menu.service;


import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.guo.dms.common.menu.dao.MenuDao;
import com.guo.dms.common.menu.entity.Menu;
import com.guo.dms.common.tree.entity.TreeNode;
import com.guo.dms.common.tree.util.TreeUtils;
import com.guo.dms.dept.user.utils.UserUtils;

@Service
public class MenuService {
	
	@Autowired
	private MenuDao menuDao;
	
	/**
	 * 将menu组合为tree结构
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public TreeNode<Menu> getMenuTree(){
		return (TreeNode<Menu>) TreeUtils.getNodeTree(menuDao.selectAllMenus(), menuDao.selectByPrimaryKey(1));
	}
	
	/**
	 * 从menuId 找出用户在此根处的节点树
	 * @param menuId
	 * @return
	 */
	public TreeNode<Menu> getCurrentUserMenuTree(Integer menuId){
		List<Menu> menus = UserUtils.getCurrentUserMenus();
		//return (TreeNode<Menu>) TreeUtils.getNodeTree(menus, menuDao.selectByPrimaryKey(menuId));
		menus.add(menuDao.selectByPrimaryKey(1));
		return  (TreeNode<Menu>) TreeUtils.getNodeTreeByHashMap(menus, 1);
	}
	
	/**
	 * 根据主键获取menu
	 * @param id
	 * @return
	 */
	public Menu getMenuById(Integer id){
		return menuDao.selectByPrimaryKey(id);
	}
	/**
	 * 添加一个menu
	 * @param menu
	 * @return
	 */
	@Transactional
	public boolean addMenu(Menu menu){
		if( menuDao.insert(menu)>0){
			return true;
		}
		return false;
	}
	/**
	 * 修改menu
	 * @param menu
	 * @return
	 */
	@Transactional
	public boolean  alertMenu(Menu menu){
		if( menuDao.updateByPrimaryKey(menu)>0){
			return true;
		}
		return false;
	}
}
